package com.kuang.controller;

import com.kuang.service.UserService;
import com.kuang.springcloud.pojo.Userinfo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
//@Controller
public class UserController {
/**
 * @description：TODO

 * @author     ：LK
 * @date       ：Created in 2021-10-26 15:11
 */
@Autowired
 UserService userService;
//获取微服务的东西
    @Autowired
  private DiscoveryClient discoveryClient;


    @Autowired
    public UserService userHystrixService;

    @GetMapping("/user/find/{username}")
    @HystrixCommand(fallbackMethod="hystrixGet")
    public Userinfo get(@PathVariable("username")String username) throws Exception {
        Userinfo userinfo = userHystrixService.getuserByUsername(username);
        if (userinfo==null){
            throw  new Exception("没有该用户，失败了，原本的方法");
        }
        return userinfo;
    }

    // 备选方法
    public Userinfo hystrixGet(@PathVariable("username")String username){
        Userinfo userinfo = new Userinfo();
        userinfo.setUsername(username+"没这个人，@Hystrix");
        return userinfo;
    }

    }
